<?php
namespace Zxf5115\Laravel\Advertising\Models\Common\Advertising;

use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Zxf5115\Laravel\Kernel\Models\Base;

/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2025-09-23
 *
 * 广告位模型类
 */
class Position extends Base
{
  /**
   * 数据库连接名
   */
  protected $connection = 'business';

  /**
   * 数据库表名
   */
  protected $table = "module_advertising_position";

  /**
   * 批量赋值允许的字段
   */
  protected $fillable = [
    'id',
  ];

  /**
   * 需要隐藏的字段
   */
  protected $hidden = [
    'update_time'
  ];


  // 关联函数 ------------------------------------------------------

  /**
   * 广告位与广告的关联
   *
   * @return [关联对象]
   */
  public function advertising() : HasMany
  {
    return $this->hasMany(
      'Zxf5115\Laravel\Advertising\Models\Common\Advertising',
      'position_id',
      'id'
    );
  }

  /**
   * 执行事件回调
   */
  protected static function boot() : void
  {
    parent::boot();

    // 删除广告位时同时删除关联的广告数据和广告详情
    static::deleted(function(self $model) : void {
      $model->advertising?->each->delete();
    });
  }
}
